<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../../assets/logo.png" alt="" />
        <span>AI果品识别后台管理系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="">
        <div @click="toggleCollapse" class="toggle-button">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#373d41"
          text-color="#fff"
          unique-opened
          :collapse="collapse"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menulist"
            v-bind:key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-location"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              v-bind:key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-location"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "AdminHome",
  data() {
    return {
      // 左侧菜单数据
      menulist: [
        {
          id: 125,
          authName: "用户管理",
          path: "users",
          children: [
            {
              id: 110,
              authName: "用户列表",
              path: "users",
              children: [],
              order: null,
            },
          ],
          order: 1,
        },
        {
          id: 126,
          authName: "图像分析管理",
          path: "users",
          children: [
            {
              id: 111,
              authName: "识别记录",
              path: "history",
              children: [],
              order: null,
            },
            // {
            //   id: 222,
            //   authName: "热搜榜单",
            //   path: "users",
            //   children: [],
            //   order: null,
            // },
          ],
          order: 2,
        },
      ],
      collapse: false,
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/adminlogin");
    },
    // 获取所有的菜单
    async getMenuList() {
      // const { data: res } = await this.$http.get("menus");
      // if (res.meta.status != 200)
      //   return this.$message.console.error(res.meta.msg);
      // this.menulist = res.data;
      // console.log(res);
    },
    // 点击按钮，切换菜单的折叠
    toggleCollapse() {
      this.collapse = !this.collapse;
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header > div {
  display: flex;
  align-items: center;
}
.el-header > div img {
  height: 3em;
}
.el-header > div span {
  margin-left: 15px;
}
.el-aside {
  background-color: #373744;
  overflow: hidden;
}
.el-aside .el-menu {
  border-right: none;
  height: 100%;
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2em;
  text-align: center;
  cursor: pointer;
}
</style>